<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>企业用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../js/formSelects/formSelects-v4.css">
</head>

<body>
<fieldset class="layui-elem-field layui-field-title" style="padding-top: 5px;border: hidden">
    <form class="layui-form" action="" lay-filter="dataForm" autocomplete=”off”>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label" >劳务公司名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="name"  placeholder="劳务公司名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">营业执照号</label>
            <div class="layui-input-block">
                <input type="text" name="licNo" id="licNo" placeholder="营业执照号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-block">
                <input type="text" name="userName" id="userName"  placeholder="联系人" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text layui-col-md11 layui-col-sm11">
            <label class="layui-form-label">手机号(企业账号)</label>
            <div class="layui-input-block">
                <input type="number" name="phone" id="phone" placeholder="手机号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-col-md11 layui-col-sm11">
            <div class="layui-input-block" style="text-align: center;">
                <button class="layui-btn" lay-submit="" id="btnSave" lay-filter="formSubmit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</fieldset>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    })
            .extend({
                formSelects: 'formSelects/formSelects-v4'
            });
    layui.use(['jquery','form','validate','common','formSelects','upload'], function () {
        var form = layui.form,
                $ = layui.jquery,
                layer = layui.layer,
                validate = layui.validate,
                formSelects = layui.formSelects,
                upload = layui.upload,
                common = layui.common;

        var id = common.getUrlParam("id");
        var type = common.getUrlParam("type");
        if (!type) type="add";  //默认为add


        if(type=="edit"){
            $.ajax({
                url: "info",
                dataType: "json",
                data: { id: id },
                success: function (rtn) {
                    if (rtn && rtn.code===0) {
                        $("#licNo").attr("readonly","readonly");
                        $("#licNo").css("background-color","#C0C0C0");
                        var data = rtn.data;
                        form.val('dataForm',{
                            name:data.name,
                            licNo:data.licNo,
                            userName:data.userName,
                            phone:data.phone
                        });
                    }
                }
            });
        }



        //监听提交
        form.on('submit(formSubmit)', function (data) {
            if(data.field.name==""){
                common.showMsg("企业名称不能为空");
                return false;
            }
            if(data.field.licNo==""){
                common.showMsg("营业执照号不能为空");
                return false;
            }
            if(data.field.userName==""){
                common.showMsg("联系人不能为空");
                return false;
            }
            if(data.field.phone==""){
                common.showMsg("手机号不能为空");
                return false;
            }
            if(!phonenull(data.field.phone)){
                common.showMsg("手机号格式不正确");
                return false;
            }
            if(id!=undefined || id!=null) {
                data.field.id = id;
            }
            $.ajax({
                type: 'post',
                url: ''+(type=="edit"?"edit":"add"),
                data: data.field,
                success: function(rtn){
                    if (rtn.code===0) {  //数据保存成功
                        var msg ="保存成功!";
                        common.showInfo(msg,function(){
                            //刷新前页
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.document.getElementById('btnSearch').click();
                            parent.layer.close(index);
                        });
                    }
                    else{
                        common.showError(rtn.msg);
                    }
                },
                error: function(XmlHttpRequest, textStatus, errorThrown){
                    common.showError('保存错误');
                }
            });
            return false;
        });
    });

    function phonenull(value) {
        if (!value) return true;
        if (value.length != 11 || !/^1\d{10}$$/.test(value)) {
            return false;
        }
        return true;
    }
</script>
</body>
</html>